/**
 * @file       PagingByObjectStrategy_test.cpp
 * @brief      TODO
 * @author     Huabo Zheng
 * @date       Jan 31, 2012
 * @Copyright  2012, www.netgear.com Inc. All rights reserved.
 */

#include "PagingByObjectStrategy.h"
#include "gtest/gtest.h"
#include "errorcode.h"
#include "PageInfo.h"
#include "Label.h"
#include "typedefine.h"
#include "Button.h"
#include "Frame.h"
#include "Rect.h"
namespace NS_LCD_AFW
{
class CPagingByObjectStrategy_test: public ::testing::Test
{
public:
	virtual void SetUp()
	{
		m_pareanRect.setLeft(0);
		m_pareanRect.setTop(0);
		m_pareanRect.setWidth(32);
		m_pareanRect.setHight(8);

		CLabel *pLabel = NULL;

		//PAGE 1 IDX 0~2
		OBJ_NEW(CLabel, pLabel);
		pLabel->create("1", CRect(3, 0, 20, 3));
		m_FramePtrArray.push_back(pLabel);

		OBJ_NEW(CLabel, pLabel);
		pLabel->create("2", CRect(3, 4, 20, 4));
		m_FramePtrArray.push_back(pLabel);

		//PAGE 2 idx 2~8
		OBJ_NEW(CLabel, pLabel);
		pLabel->create("3", CRect(3, 9, 20, 1));
		m_FramePtrArray.push_back(pLabel);
		OBJ_NEW(CLabel, pLabel);
		pLabel->create("3", CRect(3, 10, 20, 1));
		m_FramePtrArray.push_back(pLabel);
		OBJ_NEW(CLabel, pLabel);
		pLabel->create("3", CRect(3, 11, 20, 1));
		m_FramePtrArray.push_back(pLabel);
		OBJ_NEW(CLabel, pLabel);
		pLabel->create("3", CRect(3, 12, 20, 1));
		m_FramePtrArray.push_back(pLabel);
		OBJ_NEW(CLabel, pLabel);
		pLabel->create("3", CRect(3, 13, 20, 1));
		m_FramePtrArray.push_back(pLabel);
		OBJ_NEW(CLabel, pLabel);
		pLabel->create("3", CRect(3, 14, 20, 1));
		m_FramePtrArray.push_back(pLabel);
		//pLabel->create("3", CRect(3, 15, 10, 1));

		//PAGE 3 IDX 8~10
		CButton *pButton = new CButton();
		pButton->create("3", CRect(3, 16, 10, 1));
		m_FramePtrArray.push_back(pButton);
		pButton = new CButton();
		pButton->create("3", CRect(14, 16, 10, 1));
		m_FramePtrArray.push_back(pButton);

	}
	virtual void TearDown()
	{
		CFramePtrArray::iterator iter = m_FramePtrArray.begin();
		for (; iter != m_FramePtrArray.end(); ++iter)
		{
			OBJ_DESTORY((*iter));
		}
		m_FramePtrArray.clear();
	}

protected:
	CFramePtrArray m_FramePtrArray;
	CRect m_pareanRect;
};
TEST_F(CPagingByObjectStrategy_test,test_defualtConstruction)
{
}

TEST_F(CPagingByObjectStrategy_test,test_calculatePaging)
{
	CFramePtrArray emptyArray;
	emptyArray.clear();
	CPageInfo pi;
	CPagingByObjectStrategy *pStrategy = new CPagingByObjectStrategy();
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculatePaging(emptyArray,m_pareanRect,pi));
	EXPECT_EQ(1, pi.getTotalPage());
	EXPECT_EQ(1, pi.getCurPage());
	EXPECT_EQ(0, pi.getStartIndex());
	EXPECT_EQ(1, pi.getEndIndex());
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculatePaging(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(3, pi.getTotalPage());
	EXPECT_EQ(1, pi.getCurPage());
	EXPECT_EQ(0, pi.getStartIndex());
	EXPECT_EQ(2, pi.getEndIndex());
	pi.setCurPage(2);
	EXPECT_EQ(2, pi.getCurPage());
	EXPECT_EQ(2, pi.getStartIndex());
	EXPECT_EQ(8, pi.getEndIndex());

	pi.setCurPage(3);
		EXPECT_EQ(3, pi.getCurPage());
		EXPECT_EQ(8, pi.getStartIndex());
		EXPECT_EQ(10, pi.getEndIndex());
		OBJ_DELETE(pStrategy);
}
TEST_F(CPagingByObjectStrategy_test,test_findNextHightlightableObjectIndex)
{
	CPageInfo pi;
	CPagingByObjectStrategy *pStrategy = new CPagingByObjectStrategy();
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculatePaging(m_FramePtrArray,m_pareanRect,pi));
	pi.setCurPage(1);
	EXPECT_EQ(
			-1,
			pStrategy->findNextHightlightableObjectIndex(m_FramePtrArray,pi.getEndIndex() - 1,pi.getStartIndex()));
	pi.setCurPage(2);
	EXPECT_EQ(
			-1,
			pStrategy->findNextHightlightableObjectIndex(m_FramePtrArray,pi.getStartIndex(),pi.getEndIndex()));
	pi.setCurPage(3);
	EXPECT_EQ(
				8,
				pStrategy->findNextHightlightableObjectIndex(m_FramePtrArray,pi.getStartIndex(),pi.getEndIndex()));

	OBJ_DELETE(pStrategy);
}
TEST_F(CPagingByObjectStrategy_test,test_calculateViewPort)
{
	CPageInfo pi;
	CPagingByObjectStrategy *pStrategy = new CPagingByObjectStrategy();
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculatePaging(m_FramePtrArray,m_pareanRect,pi));
	pi.setDirection(CPageInfo::UNKNOW);
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(1, pi.getCurPage());
	EXPECT_TRUE(pi.isNeedSwitchPage());
	EXPECT_EQ(0, pi.getStartIndex());
	EXPECT_EQ(2, pi.getEndIndex());
	EXPECT_EQ(0, pi.getHightLightObjIndex());

	pi.setDirection(CPageInfo::UP);
	EXPECT_EQ(ERR_NO_CHANGE,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(1, pi.getCurPage());
	EXPECT_FALSE(pi.isNeedSwitchPage());
	EXPECT_EQ(0, pi.getStartIndex());
	EXPECT_EQ(2, pi.getEndIndex());
	EXPECT_EQ(0, pi.getHightLightObjIndex());

	pi.setDirection(CPageInfo::DOWN);
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(2, pi.getCurPage());
	EXPECT_TRUE(pi.isNeedSwitchPage());
	EXPECT_EQ(2, pi.getStartIndex());
	EXPECT_EQ(8, pi.getEndIndex());
	EXPECT_EQ(2, pi.getHightLightObjIndex());

	pi.setDirection(CPageInfo::DOWN);
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(3, pi.getCurPage());
	EXPECT_TRUE(pi.isNeedSwitchPage());
	EXPECT_EQ(8, pi.getStartIndex());
	EXPECT_EQ(10, pi.getEndIndex());
	EXPECT_EQ(8, pi.getHightLightObjIndex());

	pi.setDirection(CPageInfo::DOWN);
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(3, pi.getCurPage());
	EXPECT_FALSE(pi.isNeedSwitchPage());
	EXPECT_EQ(8, pi.getStartIndex());
	EXPECT_EQ(10, pi.getEndIndex());
	EXPECT_EQ(9, pi.getHightLightObjIndex());

	pi.setDirection(CPageInfo::DOWN);
	EXPECT_EQ(ERR_NO_CHANGE,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(3, pi.getCurPage());
	EXPECT_FALSE(pi.isNeedSwitchPage());
	EXPECT_EQ(8, pi.getStartIndex());
	EXPECT_EQ(10, pi.getEndIndex());
	EXPECT_EQ(9, pi.getHightLightObjIndex());


	pi.setDirection(CPageInfo::UP);
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(3, pi.getCurPage());
	EXPECT_FALSE(pi.isNeedSwitchPage());
	EXPECT_EQ(8, pi.getStartIndex());
	EXPECT_EQ(10, pi.getEndIndex());
	EXPECT_EQ(8, pi.getHightLightObjIndex());

	pi.setDirection(CPageInfo::UP);
	EXPECT_EQ(ERR_SUCCESS,
			pStrategy->calculateViewPort(m_FramePtrArray,m_pareanRect,pi));
	EXPECT_EQ(2, pi.getCurPage());
	EXPECT_TRUE(pi.isNeedSwitchPage());
	EXPECT_EQ(2, pi.getStartIndex());
	EXPECT_EQ(8, pi.getEndIndex());
	EXPECT_EQ(7, pi.getHightLightObjIndex());

	OBJ_DELETE(pStrategy);
}
} /* namespace NS_LCD_AFW */
